package com.hpjpw.idoapp.service;

import com.hpjpw.ido.domain.*;
import com.hpjpw.ido.service.*;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class IDoPersonalMomentService {
    @Autowired
    IAnonymousMomentService anonymousMomentService;
    @Autowired
    IAnonymousCommentService anonymousCommentService;
    @Autowired
    IAnonymousThumbService anonymousThumbService;
    @Autowired
    ICheerMomentService cheerMomentService;
    @Autowired
    ICheerThumbService cheerThumbService;
    @Autowired
    ICheerCommentService cheerCommentService;
    @Autowired
    ISysUserService sysUserService;
    @Autowired
    ITopicService topicService;

    /* 针对前端的个人动态请求服务，一次性返回所有加油站和撕逼墙动态相关数据，且按照时间排序 */
    public List<Map<String, Object>> personalMomentList(Long userId) {
        List<Map<String, Object>> listReturn = new ArrayList<>();
        /* 1.先加入撕逼墙动态 */
        AnonymousMoment anonymousMomentCheck = new AnonymousMoment();
        anonymousMomentCheck.setReportId(userId);
        /* 获取此用户所有撕逼墙动态以及其评论和点赞 */
        List<AnonymousMoment> anonymousMomentList = anonymousMomentService.selectAnonymousMomentList(anonymousMomentCheck);
        /* 撕逼墙只给出id content picture */
        for (AnonymousMoment item : anonymousMomentList) {
            /* status=1说明审核通过 */
            if (item.getStatus().equals("1")) {
                listReturn.add(getAnonymousMoment(item));
            }
        }

        /* 2.再加入加油站动态 */
        CheerMoment cheerMomentCheck = new CheerMoment();
        cheerMomentCheck.setReportId(userId);
        /* 获取此用户所有加油站动态以及其评论和点赞 */
        List<CheerMoment> cheerMomentList = cheerMomentService.selectCheerMomentList(cheerMomentCheck);
        /* 对每一条加油站动态插入map数据 */
        for (CheerMoment item : cheerMomentList) {
            /* 审核通过 */
            if (item.getStatus().equals("1")) {
                listReturn.add(getCheerMoment(item));
            }
        }
        /* 3.最终按发布时间重排动态列表 */
        return getMaps(listReturn);
    }

    private List<Map<String, Object>> getMaps(List<Map<String, Object>> listReturn) {
        listReturn.sort(new Comparator<Map<String, Object>>() {
            @Override
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                Date date1 = new Date(o1.get("time").toString());
                Date date2 = new Date(o2.get("time").toString());
                /*Date date2 = (Date) o2.get("time");*/
                /* >0则升序排列，需要按时间降序排列 */
                return date1.before(date2) ? 1 : -1;
            }
        });
        return listReturn;
    }

    /* 针对前端的个人动态请求服务，一次性返回所有加油站和撕逼墙动态相关数据，且按照时间排序 */
    public List<Map<String, Object>> personalCommentMomentList(Long userId) {
        List<Map<String, Object>> listReturn = new ArrayList<>();
        /* 1.增加用户评论的撕逼墙动态 */
        AnonymousComment anonymousCommentCheck = new AnonymousComment();
        anonymousCommentCheck.setFromId(userId);
        List<AnonymousComment> anonymousCommentList = anonymousCommentService.selectAnonymousCommentList(anonymousCommentCheck);
        for (AnonymousComment item : anonymousCommentList) {
            AnonymousMoment anonymousMoment = anonymousMomentService.selectAnonymousMomentById(item.getMomentId());
            if (StringUtils.isNotNull(anonymousMoment)&&anonymousMoment.getStatus().equals("1")) {
                listReturn.add(getAnonymousMoment(anonymousMoment));
            }
        }
        /* 2.增加用户评论的加油站动态 */
        CheerComment cheerCommentCheck = new CheerComment();
        cheerCommentCheck.setFromId(userId);
        List<CheerComment> cheerCommentList = cheerCommentService.selectCheerCommentList(cheerCommentCheck);
        for (CheerComment item : cheerCommentList) {
            CheerMoment cheerMoment = cheerMomentService.selectCheerMomentById(item.getMomentId());
            if (StringUtils.isNotNull(cheerMoment) && cheerMoment.getStatus().equals("1")) {
                listReturn.add(getCheerMoment(cheerMoment));
            }
        }
        /* 3.对所有动态排序 */
        return getMaps(listReturn);
    }

    /* 获取一个撕逼墙动态，并返回一组Map<String,Object> */
    public Map<String, Object> getAnonymousMoment(AnonymousMoment item) {
        Map<String, Object> map = new HashMap<>();
        /* 动态类别,0:撕逼墙，1:加油站 */
        map.put("momentType", 0);
        map.put("id", item.getId().toString());
        /* 传参和加油站相同，没有即传null */
        map.put("reportId", null);
        map.put("reportName", null);
        map.put("reportAvatar", null);
        map.put("topic", null);

        map.put("content", item.getContent());
        map.put("picture1", item.getPicture1());
        map.put("picture2", item.getPicture2());
        map.put("picture3", item.getPicture3());
        map.put("time", item.getTime());
        AnonymousThumb anonymousThumb = new AnonymousThumb();
        anonymousThumb.setMomentId(item.getId());
        List<AnonymousThumb> anonymousThumbList = anonymousThumbService.selectAnonymousThumbList(anonymousThumb);
        /* 返回所有点赞者id */
        List<Map<String, String>> mapList = new ArrayList<>();
        for (AnonymousThumb thumb : anonymousThumbList) {
            Map<String, String> stringMap = new HashMap<>();
            stringMap.put("thumbedId", thumb.getThumbedId().toString());
            stringMap.put("thumbId", null);
            stringMap.put("thumbedName", null);
            mapList.add(stringMap);
        }
        map.put("thumbs", mapList);
        /* 点赞者数量 */
        Integer thumbNum = anonymousThumbList.size();
        map.put("thumbNum", thumbNum.toString());
        /* 返回所有评论数据 */
        List<Map<String, String>> commentList = new ArrayList<>();
        AnonymousComment anonymousComment = new AnonymousComment();
        anonymousComment.setMomentId(item.getId());
        List<AnonymousComment> anonymousCommentList = anonymousCommentService.selectAnonymousCommentList(anonymousComment);
        /* 返回所有评论 */
        for (int i = 0; i < anonymousCommentList.size(); i++) {
            AnonymousComment comment = anonymousCommentList.get(i);
            Map<String, String> mapOfComment = new HashMap<>();
            mapOfComment.put("commentId", null);
            mapOfComment.put("commentatorId", null);
            mapOfComment.put("commentatorName", null);
            mapOfComment.put("commentatorAvatar", null);
            mapOfComment.put("content", comment.getContent());
            commentList.add(mapOfComment);
        }
        map.put("comments", commentList);
        /* 总评论数 */
        Integer commentNum = anonymousCommentList.size();
        map.put("commentNum", commentNum.toString());
        return map;
    }

    /* 获取一个加油站动态，并返回一组Map<String,Object> */
    public Map<String, Object> getCheerMoment(CheerMoment item) {
        Map<String, Object> map = new HashMap<>();
        /* 返回动态id，动态发布者id/姓名/头像，动态主题内容，动态内容及图片，动态发布时间 */
        map.put("momentType", 1);
        map.put("id", item.getId());
        map.put("reportId", item.getReportId());
        map.put("reportName", sysUserService.selectUserById(item.getReportId()).getNickName());
        map.put("reportAvatar", sysUserService.selectUserById(item.getReportId()).getAvatar());
        /* 没有主题则传null值 */
        if (StringUtils.isNotNull(item.getTopicId())) {
            map.put("topic", topicService.selectTopicById(item.getTopicId()).getContent());
        } else {
            map.put("topic", null);
        }
        map.put("content", item.getContent());
        map.put("picture1", item.getPicture1());
        map.put("picture2", item.getPicture2());
        map.put("picture3", item.getPicture3());
        map.put("time", item.getTime());
        /* 返回所有点赞id，点赞者id和姓名，以及点赞总数量 */
        List<Map<String, String>> thumbList = new ArrayList<>();
        CheerThumb cheerThumb = new CheerThumb();
        cheerThumb.setMomentId(item.getId());
        List<CheerThumb> cheerThumbList = cheerThumbService.selectCheerThumbList(cheerThumb);
        for (CheerThumb thumb : cheerThumbList) {
            Map<String, String> mapOfThumb = new HashMap<>();
            mapOfThumb.put("thumbedId", thumb.getThumbedId().toString());
            mapOfThumb.put("thumbId", thumb.getId());
            mapOfThumb.put("thumbedName", thumb.getThumbedName());
            thumbList.add(mapOfThumb);
        }
        map.put("thumbs", thumbList);
        map.put("thumbNum", cheerThumbList.size());
        /* 返回所有评论数据，返回具体评论id、评论者id、名称、头像、评论内容以及总评论数 */
        List<Map<String, String>> commentList = new ArrayList<>();
        CheerComment cheerComment = new CheerComment();
        cheerComment.setMomentId(item.getId());
        List<CheerComment> cheerCommentList = cheerCommentService.selectCheerCommentList(cheerComment);
        int defaultComment = cheerCommentList.size();
        for (int i = 0; i < defaultComment; i++) {
            getCheerComment(commentList, cheerCommentList, i, sysUserService);
        }
        map.put("comments", commentList);
        map.put("commentNum", cheerCommentList.size());
        return map;
    }

    static void getCheerComment(List<Map<String, String>> commentList, List<CheerComment> cheerCommentList, int i, ISysUserService sysUserService) {
        CheerComment comment = cheerCommentList.get(i);
        Map<String, String> mapOfComment = new HashMap<>();
        mapOfComment.put("commentId", comment.getId().toString());
        mapOfComment.put("commentatorId", comment.getFromId().toString());
        mapOfComment.put("commentatorName", sysUserService.selectUserById(comment.getFromId()).getNickName());
        mapOfComment.put("commentatorAvatar", sysUserService.selectUserById(comment.getFromId()).getAvatar());
        mapOfComment.put("content", comment.getContent());
        commentList.add(mapOfComment);
    }
}